package com.oscar.ehome.view.messageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.messageBean.MessageDetailBean;
import com.oscar.ehome.presenter.MessageListPreaenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.messageview.view.MessageDetailView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/7/5
 * 消息详情页
 */

public class MessageDetilActivity extends BasesActivity<MessageDetailView, MessageListPreaenter> implements MessageDetailView, ProgressFrameLayout.AgainRequestListener {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 消息详情标题
     */
    @BindView(R.id.message_detil_text)
    TextView messageDetilText;
    /**
     * 消息作者
     */
    @BindView(R.id.message_author)
    TextView messageAuthor;
    /**
     * 消息时间
     */
    @BindView(R.id.message_author_time)
    TextView messageAuthorTime;
    /**
     * 消息详情
     */
    @BindView(R.id.message_detil)
    TextView messageDetil;
    /**
     * 消息布局
     */
    @BindView(R.id.messagedetail_lay)
    LinearLayout messagedetail_lay;
    /**
     * 整体布局
     */
    @BindView(R.id.messde_fragment)
    FrameLayout messdeFragment;
    /**
     * 无网络布局
     */
    @BindView(R.id.messdetai_fragment)
    ProgressFrameLayout messdetaiFragment;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;


    String id = "";
    String token = "";
    //网络请求
    MessageListPreaenter preaenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messagedetil);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(MessageDetilActivity.this).getSystemConfig().getColor()));
        initView();
        initData();
    }

    @Override
    protected MessageListPreaenter createPresenter() {
        preaenter = new MessageListPreaenter(this);
        return preaenter;
    }

    /**
     * 初始化数据
     */
    public void initView() {
        id = getIntent().getStringExtra("ids");
        String type = getIntent().getStringExtra("type");
        titleImgBack.setVisibility(View.VISIBLE);
        titleImgBack.setImageResource(R.mipmap.fanhui);
        if (type.equals("1")) {
            titleTopbar.setText("通知");
        } else if (type.equals("2")) {
            titleTopbar.setText("激励");
        } else if (type.equals("3")) {
            titleTopbar.setText("产品");
        } else if (type.equals("4")) {
            titleTopbar.setText("会议");
        } else if (type.equals("5")) {
            titleTopbar.setText("系统");
        }
        messdetaiFragment.setAgainRequestListener(this);
        preaenter = new MessageListPreaenter(this);
    }

    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            messdetaiFragment.stopRefresh();
            messdeFragment.setVisibility(View.VISIBLE);
            token = SystemConfigFactory.getInstance(MessageDetilActivity.this).getSystemConfig().getToken();
            preaenter.messagedetailist(token, id);
            showLoading();
        } else {
            messdetaiFragment.setLoadFail();
            messdetaiFragment.setVisibility(View.VISIBLE);
            messdeFragment.setVisibility(View.GONE);
        }
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(MessageDetilActivity.this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }


    @OnClick(R.id.title_layout_left)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left:
                finish();
                break;
        }
    }

    @Override
    public void getMessageDetail(MessageDetailBean result) {
        dismissLoading();
        if (result != null) {
            messageDetilText.setText(result.getTitle());
            messageAuthor.setText(result.getCreateBy().getName());
            messageAuthorTime.setText(result.getUpdateDate());
            messageDetil.setText(result.getContent());
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(MessageDetilActivity.this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(MessageDetilActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(MessageDetilActivity.this, R.string.pull_to_refresh_network_error);
        }
    }
}
